package com.hs.health.cloud.tars.utils;

import com.qq.tars.support.log.Logger;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

public class LogUtils {

	public static final Logger LOG = Logger.getLogger("console.log");
//	public static final Logger LOG = Logger.getLogger("flow", Logger.LogType.LOCAL);

	public static void debug(String... args) {
		StackTraceElement parent = Thread.currentThread().getStackTrace()[2];

		StringBuilder sb = new StringBuilder("| ");
		sb.append( DateUtils.formatTime( new Date() ) ).append( " | " );
		sb.append(Thread.currentThread().getName()).append(" | ");
		sb.append(parent.getClassName()).append(".").append(parent.getMethodName()).append(":")
				.append(parent.getLineNumber());
		sb.append(" | ");
		for (String str :
				args) {
			sb.append(str).append(" ");
		}
		LOG.debug(sb.toString());
	}

	public static void info( String... args ) {
		StackTraceElement parent = Thread.currentThread().getStackTrace()[2];

		StringBuilder sb = new StringBuilder( "| " );
		sb.append( DateUtils.formatTime( new Date() ) ).append( " | " );
		sb.append( Thread.currentThread().getName() ).append( " | " );
		sb.append( parent.getClassName() ).append( "." ).append( parent.getMethodName() ).append( ":" )
				.append( parent.getLineNumber() );
		sb.append( " | " );
		for ( String string : args )
		{
			sb.append( string ).append( " " );
		}
		System.out.println( sb.toString() );
		LOG.info( sb.toString() );
	}

	public static void error( Exception e, String... args )
	{
		// Test Sentry
		//MonitorUtils.fixed_thread_pool.execute(new SentryCapture(e));

		StackTraceElement parent = Thread.currentThread().getStackTrace()[2];
		String trace = "";
		if ( e != null )
		{
			StringWriter sw = new StringWriter();
			e.printStackTrace( new PrintWriter( sw, true ) );
			trace = sw.toString();
		}

		StringBuilder sb = new StringBuilder("| ");
		sb.append( DateUtils.formatTime( new Date() ) ).append( " | " );
		sb.append( Thread.currentThread().getName() ).append( " | " );
		sb.append( parent.getClassName() ).append( "." ).append( parent.getMethodName() ).append( ":" )
				.append( parent.getLineNumber() );
		sb.append( " | " );
		for ( String string : args )
		{
			sb.append( string ).append( " " );
		}
		sb.append( " \n" ).append( trace );
		String log = sb.toString();
		System.err.println( log );
		LOG.error(log);
	}

//	public static class SentryCapture implements Runnable {
//		Exception e = null;
//		String env = ConfigUtils.getValue("sentry.env");
//		public SentryCapture(Exception e) {
//			this.e = e;
//		}
//		@Override
//		public void run() {
//			if (e != null) {
//				Sentry.getContext().addTag("environment", env);
//				Sentry.capture(this.e);
//			}
//		}
//	}

}
